package service

import (
	"context"
	"gitee.com/WisdomClassroom/core"

	"gitee.com/WisdomClassroom/chapter/glb"
	"gitee.com/WisdomClassroom/core/models"
	"gitee.com/WisdomClassroom/core/protobufs/v1/pb"
)

func (s Service) ListChapters(ctx context.Context, request *pb.ListChaptersRequest) (*pb.ListChaptersResponse, error) {
	chapters := make([]models.Chapter, 0, 32)
	err := glb.DB.Omit("content").Find(&chapters, "course_uuid = ?", request.GetCourseUUID()).Error
	if err != nil {
		glb.Logger.Error(err.Error())
		return listChapterRespSvrErr, nil
	}

	chaptersResp := make([]*pb.ChapterBasicInfo, 0, len(chapters))
	for _, chapter := range chapters {
		chaptersResp = append(chaptersResp, &pb.ChapterBasicInfo{
			UUID:        chapter.UUID,
			Name:        chapter.Name.String,
			Description: chapter.Description.String,
			Index:       chapter.Index.Int32,
		})
	}

	return &pb.ListChaptersResponse{
		Status:   &pb.ResponseStatus{Code: core.ResponseStatusCodeSuccess},
		Chapters: chaptersResp,
	}, nil
}
